﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Royalsoft.Common;

namespace NJL.Web.AdminVisitor
{
    public partial class EditInfoCenter :BasePage
    {
        NJL.BLL.NJL_NEWS bllNews = new BLL.NJL_NEWS();
        NJL.Model.NJL_NEWS modelNews;
        NJL.BLL.NJL_CS_NEWS_TYPE bllNewstype = new BLL.NJL_CS_NEWS_TYPE();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
               bind();
            }
        }
        void bind()
        {

            List<Model.NJL_NEWS> modelList=bllNews.GetModelList("NJL_PERSON_ID='"+UserId+"' and ID='"+Request.QueryString["id"]+"'");
            if (modelList.Count == 0)
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "请登录后再修改", "/Login.aspx");
                return;
            }           
            DataSet ds = bllNewstype.GetList("");
            ddlType.DataSource = ds;
            ddlType.DataTextField = "TYPE_NAME";
            ddlType.DataValueField = "TYPE_CODE";
            ddlType.DataBind();
            ddlType.Items.Insert(0, new ListItem("请选择", "0"));
            modelNews = bllNews.GetModel(Request.QueryString["id"].ToString());
         
            if (modelNews != null)
            {
                txtAttractionsTitle.Text = modelNews.NEWS_TITLE;
                txtAttractionFtitle.Text = modelNews.NEWS_SUBJECT_TITLE;
                txtTag.Text = modelNews.NEWS_TAG;
                txtContent.Text = modelNews.NEWS_CONTECT;
                ddlType.Items.FindByValue(modelNews.TYPE_CODE).Selected = true;
                if (modelNews.IMAGE_CODE == "")
                {
                    Image1.Visible = false;
                }
                else
                {
                    Image1.ImageUrl = GetImageUrl(modelNews.IMAGE_CODE);
                }
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            modelNews = bllNews.GetModel(Request.QueryString["id"].ToString());
            modelNews.TYPE_CODE = ddlType.SelectedValue;
            modelNews.NJL_SYS_BASE_ID = WebSiteCode;
            modelNews.NEWS_TITLE = PageValidate.InputText(txtAttractionsTitle.Text,50);
            modelNews.NEWS_SUBJECT_TITLE = PageValidate.InputText(txtAttractionFtitle.Text,50);
            modelNews.NEWS_TAG = PageValidate.InputText(txtTag.Text,100);
            modelNews.NEWS_CONTECT = txtContent.Text;
            modelNews.UPDATE_DATE_TIME = DateTime.Now;
            modelNews.UPDATE_USER_ID = UserId;
            modelNews.UPDATE_USER_NAME = UserName;
            if (bllNews.Update(modelNews))
            {
                #region 标签处理
                NJL.BLL.NJL_CS_TAG bllTag = new BLL.NJL_CS_TAG();
                NJL.Model.NJL_CS_TAG modelTag = new Model.NJL_CS_TAG();
                char[] tagtxt1 = { ',', '，' };
                string[] tag = txtTag.Text.Trim().Split(tagtxt1);
                foreach (string strtag in tag)
                {
                    DataSet ds = bllTag.GetList("TAG='" + strtag + "' and NJL_SYS_BASE_ID='" + WEBSITE_CODE + "'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        modelTag = bllTag.GetModel(ds.Tables[0].Rows[0][0].ToString());
                        if (modelTag != null)
                        {
                            if (modelTag.USED_COUNT != null)
                            {
                                modelTag.USED_COUNT += 1;
                                if (bllTag.Update(modelTag))
                                {
                                }
                            }
                        }
                    }
                    else
                    {
                        if (strtag != "")
                        {
                            modelTag.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random(Guid.NewGuid().GetHashCode()).Next(100, 999).ToString();
                            modelTag.IS_SYS_TAG = 0;
                            modelTag.TAG = strtag;
                            modelTag.CREATE_DATE_TIME = DateTime.Now;
                            modelTag.NJL_SYS_BASE_ID = WEBSITE_CODE;
                            modelTag.SEARCH_COUNT = 0;
                            modelTag.TAG_TYPE = "4";  //游记4
                            modelTag.USED_COUNT = 0;
                            bllTag.Add(modelTag);
                        }
                    }
                }
                #endregion 
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "更新成功", "InfoCenter.aspx");
            }
            
        }
        protected void btnUpload_Click1(object sender, EventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                modelNews = bllNews.GetModel(Request.QueryString["id"].ToString());
                if (modelNews != null)
                {
                    DeleteImageByCode(modelNews.IMAGE_CODE);   //删除文件
                    modelNews.IMAGE_CODE = SaveUploadFile(Request.Files);
                    if (bllNews.Update(modelNews))
                    {
                       
                        if (modelNews.IMAGE_CODE != "")
                        {
                            Image1.ImageUrl = GetImageUrl(modelNews.IMAGE_CODE);
                            Image1.Visible = true;
                        }
                        else
                        {
                            Image1.Visible = false;
                        }
                        Royalsoft.Common.MessageBox.ShowAndRedirect(this, "上传成功", "InfoCenter.aspx");
                    }

                }
            }
        }
    }
}